<?php

use app\common\model\Order;
use app\common\model\ProductCate;

function getOrderCount($uid, $status, $field = 'uid')
{
    return Order::where([$field => $uid, 'status' => $status])->count();
}

function getProductOrderFriendly($value)
{
    return match ($value) {
        1 => '商品待支付',
        2 => '商品已支付，等待商家发货',
        3 => '商家已发货，等待确认收货',
        5 => '已完成',
        6 => '订单已取消',
        default => ''
    };
}

function substrCut($userName)
{
    if (!$userName) return '未绑定';

    return hideName($userName);
}

function strreplace($str, $startlen = 0, $endlen = 4)
{
    if (!$str) return '未绑定';
    $repstr = "";
    if (strlen($str) < ($startlen + $endlen + 1)) {
        return $str;
    }
    $count = strlen($str) - $startlen - $endlen;
    for ($i = 0; $i < $count; $i++) {
        $repstr .= "*";
    }

    return preg_replace('/(\d{' . $startlen . '})\d+(\d{' . $endlen . '})/', '${1}' . $repstr . '${2}', $str);
}

function getCateTitle($id)
{
    return ProductCate::where(['id' => $id])->value('title');
}
